package com.niit.project.dao.impl;

import com.niit.project.dao.UserGoodsDao;
import com.niit.project.entity.Page;
import com.niit.project.entity.UserGoods;
import com.niit.project.utils.Dbutils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class UserGoodsDaoImpl implements UserGoodsDao {
    QueryRunner queryRunner = new QueryRunner();
    @Override
    public List<UserGoods> selectAll(Page page) {
        try {
            List<UserGoods> userGoods = queryRunner.query(Dbutils.getConnection(),"select * from usergoods limit ?,?",new BeanListHandler<UserGoods>(UserGoods.class),page.getStartRows(),page.getPageSize());
            return userGoods;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public long selectCount() {
        try {
            long count = (long) queryRunner.query(Dbutils.getConnection(),"select count(*) from usergoods",new ScalarHandler());
            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int delete(int id) {
        try {
            int result = queryRunner.update(Dbutils.getConnection(),"delete from usergoods where id = ?",id);
            queryRunner.update(Dbutils.getConnection(),"ALTER TABLE usergoods AUTO_INCREMENT = 1;");
            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int insert(UserGoods userGoods) {
        try {
            int result = queryRunner.update(Dbutils.getConnection(),"insert into usergoods  (username,goodname,goodtype,changemedium,changeway) values(?,?,?,?,?)",userGoods.getUsername(),userGoods.getGoodname(),userGoods.getGoodtype(),userGoods.getChangemedium(),userGoods.getChangeway());
            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public UserGoods select(int id) {
        try {
            UserGoods userGoods = queryRunner.query(Dbutils.getConnection(),"select * from usergoods where id = ?",new BeanHandler<UserGoods>(UserGoods.class),id);
            return userGoods;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int update(UserGoods userGoods) {
        try {
            int result = queryRunner.update(Dbutils.getConnection(),"update usergoods set username = ?,goodname = ?,goodtype = ?,changemedium= ?,changeway = ? where id= ?",userGoods.getUsername(),userGoods.getGoodname(),userGoods.getGoodtype(),userGoods.getChangemedium(),userGoods.getChangeway(),userGoods.getId());
            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public UserGoods query(String keyword) {
        try {
            System.out.println(keyword);
            UserGoods objects = queryRunner.query(Dbutils.getConnection(),"select * from usergoods where goodname like '%'?'%' and goodtype like '%'?'%' or changemedium like '%'?'%' or changeway like '%'?'%';",new BeanHandler<UserGoods>(UserGoods.class),keyword,keyword,keyword,keyword);
            if(objects==null)
            {
                System.out.println("找不到该商品！！");
            }
            else {
                System.out.println("this answers is " + objects);
            }

            return objects;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }



}
